package cn.tsoft.handler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class HandlerTest extends Activity {
    
	private ProgressBar proger =null;
	private Button  button =null;
	private Button  stop =null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        proger = (ProgressBar) findViewById(R.id.progress1);
        button  =(Button)findViewById(R.id.start);
        stop= (Button)findViewById(R.id.stop);
        setViewListener();
    }

	private void setViewListener() {
		button.setOnClickListener(new Button.OnClickListener()   
        {   
            public void onClick(View v) {   
            	proger.setVisibility(View.VISIBLE);   
            	proger.setProgress(0);
            	proger.setMax(100);
                handler.post(handlerThread);   
            }   
        });
        
        stop.setOnClickListener(new Button.OnClickListener()   
        {   
            public void onClick(View v) {   
               handler.sendEmptyMessage(1); 
               Intent intent =new Intent();
   			   intent.setClass(HandlerTest.this, HandlerTest01.class);
			  //通过Intent对象启动另外一个Activity
   			   HandlerTest.this.startActivity(intent);
   			   HandlerTest.this.finish();
            }   
        });
	}
	
	Handler handler = new Handler()   
    {   
        public void handleMessage(Message msg)   
        {  
        	switch(msg.what) {
	    		case 1 : 
	    			proger.setProgress(0);
	    			proger.setVisibility(View.GONE);
	    			handler.removeCallbacks(handlerThread);
	    		    break;
	        	default :
	        		proger.setProgress(msg.arg1);   
                	handler.post(handlerThread);
	        		break;
        	}
        }   
    };  
    
  //线程类，该类使用的是匿名内部类的方式进行声明   
    Runnable handlerThread = new Runnable()   
    {   
        int i = 0;   
        public void run()   
        {
            i = i + 10;   
            /**  
             * 得到一个消息对象，Message类是由android操作系统提供  
             * obtainMessage方法用来得到Message对象  
             */  
            Message msg = handler.obtainMessage();   
            /**  
             * Message中有个成员变量，即msg独享的arg1参数  
             * 将其值设置为i。用arg1或arg2这两个成员变量传递  
             * 消息，优点是系统性能消耗较少  
             */  
            msg.arg1 = i;   
            try {   
                //当前线程休眠1秒   
                Thread.sleep(1000);   
            } catch (InterruptedException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
            /**  
             * 发送一个消息，用sendMessage是将msg加入到消息  
             * 队列中。而post是将线程加入到线程队列中  
             */  
            handler.sendMessage(msg);   
            if( i == 100)   
            {   
                /**  
                 * 如果i=100的时候，就将线程对象  
                 * 从handler当中移除  
                 */  
            	msg.arg1 = 0;
            	handler.sendEmptyMessage(1); 
            }   
        }   
    };   


}